<template>
  <div class="content-wraper">
    <div class="search-box">
      <el-form label-width="100px" :inline="true">
        <el-form-item label="项目名称">
          <el-input v-model="projectName" />
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="search()"
        >查询</el-button>
        <el-button
          type="warning"
          icon="el-icon-refresh"
          @click="reset()"
        >重置</el-button>
        <el-button
          type="success"
          icon="el-icon-plus"
          @click="addProjectFundPlanInfo()"
        >新增</el-button>
      </el-form>
    </div>
    <div class="detail">
      <com-table
        :data="dataList"
        :columns="columns"
        :total="total"
        @pageChange="pageChange"
      >
        <template v-slot:index="{ row: index }">
          <div>{{ index }}</div>
        </template>
        <!-- <el-table-column type="index" width="50"> </el-table-column> -->
        <template v-slot:name="{ row }">
          <a class="com-link" @click="viewDetail(row)">{{ row.name }}</a>
        </template>
        <template v-slot:options="{ row }">
          <el-button
            type="success"
            icon="el-icon-edit-outline"
            size="mini"
            @click="startProcess(row)"
          >提交</el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="editProjectFundPlanInfo(row)"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delProjectFundPlanInfo(row)"
          >删除</el-button>
        </template>
        <template v-slot:status="{row}">
          <a class="com-link" style="text-decoration:underline;" @click="getWorkflowDetail(row)">{{ row.status | formateFlowStatus }}</a>
        </template>
        <template v-slot:createTime="{ row: { createTime } }">
          {{ dateFormat(createTime) }}
        </template>
      </com-table>
    </div>
    <add-project-fund-plan-info
      :show-modal.sync="showModal"
      :opt-type="optType"
      :data="unitDetail"
      @freshList="search"
    />
    <process-start :show-modal.sync="showStartModal" :currentrow="unitDetail" @freshList="search" />
    <workflow-schedule :show-modal.sync="showWorkflowSchedule" :data="workflowScheduleData" />
  </div>
</template>

<script>
import workflowSchedule from '@/components/TreeModal/workflowSchedule'
import {
  deleteProjectFundPlanInfo,
  getProjectFundPlanBasicInfoList
} from '@/api/moneyManage'
import moment from 'moment'
import { getDictName } from '@/utils/comm'
import addProjectFundPlanInfo from './addProjectFundPlanInfo.vue'
import { workflowStatus } from '@/utils/dicData'
export default {
  name: 'ProjectFundPlanInfo',
  components: {
    addProjectFundPlanInfo,
    workflowSchedule
  },
  data() {
    return {
      projectName: '',
      getDictName,
      workflowStatus,
      showStartModal: false,
      dataList: [],
      total: 0,
      pageParams: {
        pageNum: 1,
        pageSize: 10
      },
      showWorkflowSchedule: false,
      workflowScheduleData: {},
      showModal: false,
      optType: 'add',
      unitDetail: {},
      columns: [
        {
          label: '编号',
          prop: 'index',
          width: '60'
        },
        {
          label: '项目名称',
          prop: 'projectName',
          width: '300'
        },
        {
          label: '年度月份信息',
          prop: 'yearMonthInfo'
        },
        {
          label: '本月计划支付金额',
          prop: 'paymentAmount'
        },
        {
          label: '本月公司核定金额',
          prop: 'approvedAmount'
        },
        {
          label: '备注',
          prop: 'remark'
        },
        {
          label: '流程状态',
          prop: 'status',
          slot: 'status'
        },
        {
          label: '编制人',
          prop: 'founderName'
        },
        {
          label: '编制时间',
          prop: 'createTime',
          slot: 'createTime',
          width: '90'
        },

        {
          fixed: 'right',
          label: '操作',
          width: '260',
          prop: 'options',
          slot: 'options'
        }
      ]
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getWorkflowDetail(data) {
      this.showWorkflowSchedule = true
      this.workflowScheduleData = data
    },
    startProcess(data) {
      this.unitDetail = data
      this.showStartModal = true
    },
    dateFormat(data) {
      if (data) {
        return moment(data).format('YYYY-MM-DD')
      } else {
        return ''
      }
    },
    pageChange(data) {
      this.pageParams = data
      this.getDataList()
    },
    getDataList() {
      const params = {
        ...this.pageParams,
        projectName: this.projectName
      }
      getProjectFundPlanBasicInfoList(params).then((res) => {
        const { data, total } = res.data || {}
        if (data) {
          this.dataList = data
          this.dataList.forEach((element, index) => {
            element.index = index + 1
          })
          this.total = total - 0
        }
      })
    },
    addProjectFundPlanInfo() {
      this.optType = 'add'
      this.showModal = true
    },
    editProjectFundPlanInfo(data) {
      this.optType = 'edit'
      this.unitDetail = data
      this.showModal = true
    },
    viewDetail(data) {
      this.optType = 'detail'
      this.unitDetail = data
      this.showAddFileModal = true
    },
    delProjectFundPlanInfo(data) {
      this.$confirm('确定要删除该项目月度资金计划吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const { id } = data
          deleteProjectFundPlanInfo({
            projectFundPlanInfoId: id
          }).then((res) => {
            const { code, msg } = res
            if (code === '0001') {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getDataList()
            } else {
              this.$message({
                type: 'error',
                message: msg || '删除失败!'
              })
            }
          })
        })
        .catch(() => {})
    },
    search() {
      this.pageParams.pageNum = 1
      this.getDataList()
    },
    reset() {
      this.projectName = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.search-box {
  height: 50px;
}
.detail {
  height: calc(100% - 50px);
}
</style>
